<template>
  <div class="xtx-goods-page">
    <!-- v-if="goodDetail.categories" 规避默认第一次加载数据不存在报错问题 -->
    <div class="container" v-if="goodDetail.categories">
      <!-- 面包屑 -->
      <XtxBread separator="/">
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <!-- 一级分类 -->
        <XtxBreadItem to="/">{{ goodDetail.categories[1].name }}</XtxBreadItem>
        <!-- 二级分类 -->
        <XtxBreadItem to="/">{{ goodDetail.categories[0].name }}</XtxBreadItem>
        <!-- 商品的名称 -->
        <XtxBreadItem to="/">{{ goodDetail.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 图片预览区 -->
        <div class="media">
          <XtxGoodViewer :imageList="goodDetail.mainPictures" />
          <!-- 商品人气 -->
          <GoodSale />
        </div>
        <!-- 商品信息区 -->
        <div class="spec">
          <GoodInfo />
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <div class="goods-tabs">
            <!-- 详情图片列表 -->
            <img />
          </div>
        </div>
        <div class="goods-aside"></div>
      </div>
    </div>
  </div>
</template>

<script>
import { findGoodsDetailApi } from '@/api/good'
import { onMounted, ref, provide } from 'vue'
import { useRoute } from 'vue-router'
// 导入商品信息
import GoodInfo from './components/goods-name.vue'
import GoodSale from './components/goods-sales.vue'
export default {
  name: 'XtxGoodsPage',
  components: {
    GoodInfo,
    GoodSale
  },
  setup () {
    // 存储商品详情
    const goodDetail = ref({})

    // 注入=>注意：不能嵌套
    provide('detail', goodDetail)

    // 获取商品ID
    const route = useRoute()
    // 1. 获取商品详情数据
    const getGoodDetail = async (id) => {
      const { data } = await findGoodsDetailApi(id)
      console.log('商品详情：', data)
      goodDetail.value = data
    }
    onMounted(() => {
      getGoodDetail(route.params.id)
    })

    return { goodDetail }
  }
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
    background: #fff;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
  img {
    width: 100%;
  }
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
